package edu.hrbeu.crc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.hrbeu.crc.common.helper.ListItemHelper;
import edu.hrbeu.crc.constant.StatusConstant;
import edu.hrbeu.crc.mapper.ShoppingItemMapper;
import edu.hrbeu.crc.pojo.dto.ShoppingItemDTO;
import edu.hrbeu.crc.pojo.entity.Category;
import edu.hrbeu.crc.pojo.entity.ShoppingItem;
import edu.hrbeu.crc.pojo.vo.ShoppingItemVO;
import edu.hrbeu.crc.service.CategoryService;
import edu.hrbeu.crc.service.ShoppingItemService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingItemServiceImpl extends ServiceImpl<ShoppingItemMapper, ShoppingItem> implements ShoppingItemService {

    @Resource
    private CategoryService categoryService;

    @Resource
    private ListItemHelper listItemHelper;

    /**
     * 添加商品
     * @param shoppingItemDTO
     */
    @Override
    public void add(ShoppingItemDTO shoppingItemDTO) {
        ShoppingItem shoppingItem = new ShoppingItem();
        BeanUtils.copyProperties(shoppingItemDTO, shoppingItem);

        Category c = categoryService.getByName(shoppingItemDTO.getCategoryName());
        shoppingItem.setCategoryId(c.getId());
        shoppingItem.setCreateTime(LocalDateTime.now());
        shoppingItem.setUpdateTime(LocalDateTime.now());
        this.save(shoppingItem);
    }

    /**
     * 标记商品是否购买
     * @param itemId
     * @param status
     */
    @Override
    @Transactional
    public void updateStatus(Integer itemId, Integer status) {
        UpdateWrapper<ShoppingItem> wrapper = new UpdateWrapper<>();
        LocalDateTime now = LocalDateTime.now();
        wrapper.lambda()
                .set(ShoppingItem::getStatus,status)
                .set(ShoppingItem::getUpdateTime,now)
                .eq(ShoppingItem::getId,itemId);
        this.update(wrapper);

        ShoppingItem item = this.getById(itemId);
        listItemHelper.checkAndUpdateListStatus(item.getListId());
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @Override
    public ShoppingItemVO getItem(Integer id) {
        ShoppingItem item = this.getById(id);
        ShoppingItemVO vo = BeanUtil.copyProperties(item, ShoppingItemVO.class);

        Category c = categoryService.getById(vo.getCategoryId());
        vo.setCategoryName(c.getName());
        vo.setCategoryLogo(c.getLogo());

        return vo;
    }

    /**
     * 修改商品
     * @param shoppingItemDTO
     */
    @Override
    public void updateItem(ShoppingItemDTO shoppingItemDTO) {
        Category c = categoryService.getByName(shoppingItemDTO.getCategoryName());
        // 修改更新时间
        LocalDateTime now = LocalDateTime.now();
        UpdateWrapper<ShoppingItem> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .set(shoppingItemDTO.getName() != null, ShoppingItem::getName, shoppingItemDTO.getName())
                .set(shoppingItemDTO.getQuantity() != null, ShoppingItem::getQuantity,shoppingItemDTO.getQuantity())
                .set(shoppingItemDTO.getPrice() != null, ShoppingItem::getPrice,shoppingItemDTO.getPrice())
                .set(c.getId() != null, ShoppingItem::getCategoryId,c.getId())
                .set(ShoppingItem::getUpdateTime,now)
                .eq(ShoppingItem::getId,shoppingItemDTO.getItemId());
        update(wrapper);
    }

    /**
     * 批量删除
     * @param itemIds
     */
    @Transactional
    @Override
    public void delete(List<Integer> itemIds) {
        this.removeByIds(itemIds);
    }
}
